package com.ytktt.study.week3.task346.gateway.router;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 轮询
 *
 * @author ytk
 */
public class RoundRibbonHttpEndpointRouter implements HttpEndpointRouter {

    private final AtomicInteger num;

    public RoundRibbonHttpEndpointRouter(AtomicInteger roundRibbonNum) {
        this.num = roundRibbonNum;
    }

    @Override
    public String route(List<String> endpoints) {
        long index = num.getAndUpdate(cur -> (cur + 1) & 0x7FFFFFFF);
        return endpoints.get((int) (index % endpoints.size()));
    }
}
